#include "GameScreen.h"
#include "HelloWorldScene.h"

using namespace cocos2d;

GameScreen::GameScreen()
{

}

GameScreen::~GameScreen()
{


	// cpp don't need to call super dealloc
	// virtual destructor will do this
}

CCScene* GameScreen::scene()
{
	CCScene * scene = NULL;
	do 
	{
		scene = CCScene::node();
		CC_BREAK_IF(! scene);

		GameScreen *layer = GameScreen::node();
		CC_BREAK_IF(! layer);

		scene->addChild(layer);
	} while (0);

	return scene;
}

bool GameScreen::init()
{
	bool bRet = false;
	do 
	{

		CC_BREAK_IF(! CCLayer::init());

		setIsTouchEnabled(true);

		//player = CCSprite::spriteWithFile("sprite/player.png");
		player = Sprite3D::spriteWithFile(linkFileName("sprite/", "fish.png"),linkFileName("sprite/", "fish.mesh"));
		CC_BREAK_IF(! player);

		CCSize size = CCDirector::sharedDirector()->getWinSize();
		player->setPosition(ccp(size.width/2, size.height/2));
		player_v = 0;
		player_a = 9.8f;

		this->addChild(player, 0, 1);

		//CCTMXTiledMap* tileMap = CCTMXTiledMap::tiledMapWithTMXFile(linkFileName("map/level/", "test1.tmx"));
		tileMap = CCTMXTiledMap::tiledMapWithTMXFile( linkFileName("map/level/", "ocean_1.tmx"));
		//tileMap->setScale(0.5f);
		map_x = 0;
		map_y = -700;
		tileMap->setPosition(ccp(map_x, map_y));
		this->addChild(tileMap, -1, 2);


		this->scheduleUpdate();


		bRet = true;
	} while (0);

	return bRet;

}

void GameScreen::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{

	CCSetIterator it = pTouches->begin();
	CCTouch* touch = (CCTouch*)(*it);

	CCPoint touchLocation = touch->locationInView( touch->view() );	
	touchLocation = CCDirector::sharedDirector()->convertToGL( touchLocation );
	CCLog("BeganX=%f",touchLocation.x);

	//player->setPosition(touchLocation);
	player_a = -9.8f;
}

void GameScreen::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
{
	CCSetIterator it = pTouches->begin();
	CCTouch* touch = (CCTouch*)(*it);

	CCPoint touchLocation = touch->locationInView( touch->view() );	
	touchLocation = CCDirector::sharedDirector()->convertToGL( touchLocation );
	CCLog("MovedX=%f",touchLocation.x);

	//player->setPosition(touchLocation);
}

void GameScreen::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{
	CCSetIterator it = pTouches->begin();
	CCTouch* touch = (CCTouch*)(*it);

	CCPoint touchLocation = touch->locationInView( touch->view() );	
	touchLocation = CCDirector::sharedDirector()->convertToGL( touchLocation );
	CCLog("EndedX=%f",touchLocation.x);

	//player->setPosition(touchLocation);
	player_a = 9.8f;

	/*if(touchLocation.x > player->getPosition().x - 32 
		&& touchLocation.x < player->getPosition().x + 32 
		&& touchLocation.y > player->getPosition().y - 32 
		&& touchLocation.y < player->getPosition().y + 32 )
	{
		CCScene *pScene = HelloWorld::scene();
		CCDirector::sharedDirector()->replaceScene(pScene);
	}*/
}

void GameScreen::update(ccTime delta)
{

	//CCLog("delta=%f",delta);

	//updatePlayer(delta);
	player->update(delta);



	map_x -= 2;
	if(map_x <= -tileMap->getMapSize().width * tileMap->getTileSize().width)
		map_x = 0;
	tileMap->setPosition(ccp(map_x, map_y));
}

void GameScreen::updatePlayer(ccTime delta)
{

	player_v = player_v + player_a * delta / 2;


	CCPoint pos;
	pos.x = player->getPosition().x;
	pos.y = player->getPosition().y-player_v;
	if(pos.y < 0)
	{
		pos.y = 0;
		player_v = 0;
	}
	CCSize size = CCDirector::sharedDirector()->getWinSize();
	if(pos.y > size.height)
	{
		pos.y = size.height;
		player_v = 0;
	}
	player->setPosition(pos);

}

void GameScreen::onEnter()
{

	CCLayer::onEnter();
}

void GameScreen::onExit()
{

	CCLayer::onExit();
}

void GameScreen::onEnterTransitionDidFinish()
{

	CCLayer::onEnterTransitionDidFinish();
}


char* GameScreen::linkFileName(char *pathname, char *filename)
{
#ifdef WIN32
	char *rt = (char *)malloc(sizeof(char)*(strlen(pathname)+strlen(filename)));
	strcpy(rt,pathname);
	return strcat(rt,filename);
#else
	return filename;
#endif
}